package com.xinqi.modules.file.course.prepare.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.xinqi.common.base.dto.CommonFileDTO;
import com.xinqi.common.base.utils.json.JSON;
import com.xinqi.common.mybatis.data.service.impl.BaseServiceImpl;
import com.xinqi.common.rocketmq.dto.MessageBody;
import com.xinqi.modules.file.course.prepare.domain.PrepareCoursewareEntity;
import com.xinqi.modules.file.course.prepare.domain.PrepareCoursewarePartEntity;
import com.xinqi.modules.file.course.prepare.mapper.PrepareCoursewareMapper;
import com.xinqi.modules.file.course.prepare.mapper.PrepareCoursewarePartMapper;
import com.xinqi.modules.file.course.prepare.service.PrepareService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class PrepareServiceImpl extends BaseServiceImpl<PrepareCoursewarePartMapper, PrepareCoursewarePartEntity> implements PrepareService {

    private final PrepareCoursewareMapper prepareCoursewareMapper;

    private final PrepareCoursewarePartMapper prepareCoursewarePartMapper;

    @Override
    public boolean sliceFile(MessageBody body) {
        return false;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean sliceOssFile(Long key, List<String> fileUrls) {
        PrepareCoursewareEntity entity = prepareCoursewareMapper.selectById(key);
        if (Objects.isNull(entity) || CollectionUtils.isEmpty(fileUrls)) {
            return true;
        }
        // 删除已有切片
        LambdaQueryWrapper<PrepareCoursewarePartEntity> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(PrepareCoursewarePartEntity::getCoursewareId, entity.getId());
        List<PrepareCoursewarePartEntity> oldPartList = prepareCoursewarePartMapper.selectList(wrapper);
        if (CollectionUtils.isNotEmpty(oldPartList)) {
            prepareCoursewarePartMapper.deleteByIds(oldPartList.stream().map(PrepareCoursewarePartEntity::getId).collect(Collectors.toList()));
        }
        // 添加新切片
        List<CommonFileDTO> fileList = JSON.parseList(entity.getFiles(), CommonFileDTO.class);
        if (CollectionUtils.isNotEmpty(fileUrls)) {
            List<PrepareCoursewarePartEntity> partList = new ArrayList<>();
            for (int i = 0; i < fileUrls.size(); i++) {
                String url = fileUrls.get(i);
                PrepareCoursewarePartEntity part = new PrepareCoursewarePartEntity();
                part.setPrepareId(entity.getPrepareId());
                part.setCoursewareId(key);
                part.setFileType(String.valueOf(fileList.get(0).getType()));
                part.setPageNumber(i + 1);
                part.setName("第" + (i + 1) + "个切片");
                part.setTotalScore(BigDecimal.ZERO);
                part.setQuestionTypeTotal(0);
                part.setQuestionTotal(0);

                CommonFileDTO file = new CommonFileDTO();
                file.setOssFilename(part.getName());
                file.setFilename(part.getName());
                file.setType(Integer.valueOf(part.getFileType()));
                file.setUrl(url);
                file.setFileUrl(url);
                part.setFile(JSON.stringify(file));

                partList.add(part);
            }
            this.saveBatch(partList);
        }
        return true;
    }
}
